package com.business.dynamic.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.business.common.utils.StrKit;
import com.business.dynamic.entity.DynamicDefaultAttribute;
import com.business.dynamic.mapper.DynamicDefaultAttributeMapper;
import com.business.dynamic.service.DynamicDefaultAttributeService;
import com.business.redis.ExpiresTimeEnum;
import com.business.redis.constant.RedisKeyConstant;
import com.business.redis.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 动态默认属性表:业务接口实现类
 *
 * @author chenjiehong
 * @date 2019-11-29 15:15:38
 * @since 1.0
 */
@Service
public class DynamicDefaultAttributeServiceImpl implements DynamicDefaultAttributeService {

    @Autowired
    private DynamicDefaultAttributeMapper dynamicDefaultAttributeMapper;


    @Override
    public List<DynamicDefaultAttribute> queryList(Integer type, String parentCategoryCode, String subCategoryCode) {
        List<DynamicDefaultAttribute> result = null;
        String key = null;
        if (StrKit.isNotEmpty(subCategoryCode)) {
            key = String.format(RedisKeyConstant.DYNAMIC_DEFAULT_ATTRIBUTE_QUERYLIST1, type, parentCategoryCode, subCategoryCode);
        } else if (StrKit.isNotEmpty(parentCategoryCode)) {
            key = String.format(RedisKeyConstant.DYNAMIC_DEFAULT_ATTRIBUTE_QUERYLIST2, type, parentCategoryCode);
        } else {
            key = String.format(RedisKeyConstant.DYNAMIC_DEFAULT_ATTRIBUTE_QUERYLIST3, type);
        }
        if (RedisUtil.hasKey(key)) {
            return RedisUtil.getArray(key, DynamicDefaultAttribute.class);
        } else {
            Wrapper<DynamicDefaultAttribute> wrapper = new EntityWrapper<>();
            wrapper.setSqlSelect("id,name,default_value,unit,value_type,is_required,regular_expression,is_scope,dictionary_type_id,error_message");
            wrapper.eq("state", 1);
            wrapper.and(" ( type = {0} and ( parent_category_code = {1} or sub_category_code = {2} )) or ( type = {0} and parent_category_code = '' ) ", type, parentCategoryCode, subCategoryCode);
            result = dynamicDefaultAttributeMapper.selectList(wrapper);
            RedisUtil.setValue(key, result);
        }
        return result;
    }


}